package com.xc.h05.service.weichen.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xc.h05.entity.Heartmark;
import com.xc.h05.entity.Mytest;
import com.xc.h05.entity.User;
import com.xc.h05.mapper.HeartmarkMapper;
import com.xc.h05.mapper.MytestMapper;
import com.xc.h05.mapper.UserMapper;
import com.xc.h05.service.weichen.MyTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MyTestServiceImpl implements MyTestService{
    @Autowired
    private MytestMapper mytestMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private HeartmarkMapper heartmarkMapper;

    @Override
    public int add(Mytest mytest) {
        mytest.setTestTime(new Date());
        int add = mytestMapper.add(mytest);
        return add;
    }

    @Override
    public int delete(int id) {
        int delete = mytestMapper.delete(id);
        return delete;
    }

    @Override
    public int deletes(List list) {
        int deletes = mytestMapper.deletes(list);
        return deletes;
    }

    @Override
    public int update(Mytest mytest) {
        int update = mytestMapper.update(mytest);
        return update;
    }

    @Override
    public PageInfo<Mytest> findByPage(int pageNum, int pageSize, Mytest mytest) {
        PageHelper.startPage(pageNum,pageSize);
        List<Mytest> list = mytestMapper.findByPage(mytest);
        for (Mytest mt : list) {
            User user = userMapper.findById(mt.getUserId());
            mt.setUser(user);
            Heartmark heartmark = heartmarkMapper.findById(mt.getTestId());
            mt.setHeartmark(heartmark);
        }
        PageInfo<Mytest> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<Mytest> findByNicknamePage(int pageNum, int pageSize, Mytest mytest) {
        PageHelper.startPage(pageNum,pageSize);
        List<Mytest> list = mytestMapper.findByNicknamePage(mytest);
        for (Mytest mt : list) {
            User user = userMapper.findById(mt.getUserId());
            mt.setUser(user);
            Heartmark heartmark = heartmarkMapper.findById(mt.getTestId());
            mt.setHeartmark(heartmark);
        }
        PageInfo<Mytest> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<Mytest> findByTestId(int testId) {
        List<Mytest> list = mytestMapper.findByTestId(testId);
        return list;
    }
}
